package java6681.bank;

/**
 * @author Junichiro
 * @description 实现Runnable接口创建线程任务类
 * @date 2021-06-05 18:57
 **/

public class TicketRunnable6681 {
    private static int tickets = 1;

    public static void main(String[] args) {
        new Thread(TicketRunnable6681::saleByRunnable6681,"张舒婷1").start();
        new Thread(TicketRunnable6681::saleByRunnable6681,"张舒婷2").start();
        new Thread(TicketRunnable6681::saleByRunnable6681,"张舒婷3").start();
    }

    private static void saleByRunnable6681() {
        while (true) {
            synchronized (TicketRunnable6681.class) {
                if (tickets <= 8) {
                    System.out.println(Thread.currentThread().getName() +
                            "已售出第" + tickets++ + "张票");
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
